package com.kc.sjwh.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kc.sjwh.dao.SysDao;
import com.kc.sjwh.dao.VipDao;
import com.kc.sjwh.entity.po.Vip;
import com.kc.sjwh.entity.vo.dict.DictItem;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.other.Tool;

/**
 * VIP service
 * @author xianding
 * @date 2017年11月28日
 */
@Service
public class VipService {
	static Logger logger = Logger.getLogger(VipService.class);
	@Autowired
	VipDao vipDao;
	@Autowired
	SysDao sysDao;

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageNum
	 * @param param
	 * @return
	 */
	public Page<Vip> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("t.gxsj desc");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("startTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t.gxsj >= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("endTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t.gxsj <= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("t.mc like ?  or t.dhhm like ? or t.ms like ?".replace("?", "'%"+v+"%'"));
		}
		v = param.get("dwdm");
		if(StringUtils.isNotEmpty(v)) {
			q.put("t.ssdwdm", v);
		}
		
		return vipDao.getPage("get", q);
	}

	/**
	 * 新增
	 * @param one
	 */
	public void add(Vip one) {
		Vip chk_n=vipDao.getOne("getN",new QueryParam().put("t.dhhm", one.getDhhm()));
		if (chk_n!=null) {				
			vipDao.delete(chk_n.getBh());			
		}
		Vip chk=vipDao.getOne(new QueryParam().put("t.dhhm", one.getDhhm()));
		if (chk!=null) {				
			throw new IllegalArgumentException("此电话号码已经存在");			
		}
		one.setBh(Tool.getUuid());
		vipDao.add(one);
	}

	/**
	 * 修改
	 * @param one
	 */
	public void update(Vip one) {
		Vip chk_n=vipDao.getOne("getN",new QueryParam().put("t.dhhm", one.getDhhm()));
		if (chk_n!=null) {				
			vipDao.delete(chk_n.getBh());			
		}
		Vip chk=vipDao.getOne(new QueryParam().put("t.dhhm", one.getDhhm()));
		if (chk!=null&&(!chk.getBh().equals(one.getBh()))) {				
			throw new IllegalArgumentException("此电话号码已经存在");			
		}
		one.setGxsj(new Date());
		vipDao.update(one, "bh");
	}

	/**
	 * 删除
	 * @param id
	 */
	@Transactional
	public void delete(String id) {
		//级联删除 none
		Vip one=new Vip();
		one.setBh(id);
		one.setSybz("N");
		vipDao.update(one, "bh");
		//删除
//		int n = vipDao.delete(id);
//		if(n == 0) {
//			throw new IllegalArgumentException("要删除的记录不存在");
//		}
	}

	/**
	 * 打开表单
	 * @param id
	 * @param user 
	 * @return
	 */
	public Vip form(String id) {
		if (id!=null) {
			Vip one=vipDao.getOne(new QueryParam().put("bh", id));
			List<DictItem> list=sysDao.getDict("行政区划");
			for (DictItem dictItem : list) {
				if (dictItem.getId().equals(one.getXzqh())) {
					one.setXzqhmc(dictItem.getName());
					break;
				}
			}
			return one;
		}		
		return  null;
	}

	/**
	 * 根据行设置处置力量页
	 */
//	public void setVipByRow(HSSFRow row, Vip one) {
//		
//	}

	/**
	 * 取所有页为字典
	 * 
	 * @return
	 */
	public List<DictItem> getAllAsDict() {
		return null;
	}

}
